package cn.th.controller;


import cn.th.common.R;
import cn.th.dto.AdDto;
import cn.th.entity.Dormitory;
import cn.th.service.IDormitoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.Map;

/**
 * <p>
 * 宿舍表 前端控制器
 * </p>
 *
 * @author cn.th
 * @since 2024-03-30
 */
@Slf4j
@RestController
@RequestMapping("/dormitory")
public class DormitoryController {

    @Autowired
    private IDormitoryService dormitoryService;

    /**
     * 查询宿舍
     * @param page
     * @param pageSize
     * @param school
     * @param building
     * @param number
     * @param total
     * @return
     */
    @GetMapping("/list")
    public R<Map<String, Object>> page(@RequestParam(value = "page") int page,
                                       @RequestParam(value = "pageSize") int pageSize,
                                       @RequestParam(value = "school", required = false) String school,
                                       @RequestParam(value = "building", required = false) String building,
                                       @RequestParam(value = "number", required = false) String number,
                                       @RequestParam(value = "total", required = false) Integer total){

        Map<String, Object> data = dormitoryService.queryDormitory(page,pageSize,school,building,number,total);
        return R.success(data);
    }

    /**
     * 新增宿舍，使用excel表格导入
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public R<String> uploadExcel(@RequestParam("file") MultipartFile file) {
        dormitoryService.saveFile(file);
        return R.success("新增宿舍成功！！！");
    }

    /**
     * 删除宿舍，可批量删除
     * @param ids
     */
    @DeleteMapping
    public R<String> delete(String ids){
        dormitoryService.removeBatchByIds(ids);
        return R.success("删除宿舍成功");
    }

    /**
     * 修改宿舍
     * @param dormitory
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Dormitory dormitory){

        log.info(dormitory.toString());

        dormitoryService.updateById(dormitory);
        return R.success("修改宿舍成功");
    }

    /**
     * 修改宿舍时页面的数据展示
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Dormitory> getDormitoryById(@PathVariable("id")Long id){
        Dormitory dormitory = dormitoryService.getById(id);
        return R.success(dormitory);
    }
}

